<template>
  <div class="user-interest">
    <h2>兴趣设置</h2>
    <div class="interest-form">
      <div 
        v-for="(interest, index) in interests" 
        :key="index"
        class="interest-option"
        :class="{ selected: selectedInterests.includes(interest) }"
        @click="toggleInterest(interest)"
      >
        {{ interest }}
      </div>
    </div>
    <button class="submit-btn" @click="modifyUserInterestData">保存兴趣设置</button>
  </div>
</template>

<script>
export default {
  name: "UserInterest",
  components: {},
  data() {
    return {
      interests: [
        "房产", "理财", "艺术", "文化", "游戏", "时尚", "教育", 
        "美国职业篮球", "国际足球", "国内足球", "中国职业篮球", "手机", 
        "家电", "科技", "电视剧", "音乐", "电影", "人工智能", "数码", 
        "国内", "国际", "航空", "军事", "股票", "商业", "基金"
      ],
      selectedInterests: [],
    }
  },
  methods: {
    toggleInterest(interest) {
      const index = this.selectedInterests.indexOf(interest);
      if (index === -1) {
        this.selectedInterests.push(interest);
      } else {
        this.selectedInterests.splice(index, 1);
      }
    },
    queryUserInterestData() {
      this.$api.serviceUser.post('api/user/queryUserInterest', {'userAccount': this.$store.state.user.userAccount}, {
        headers: {
          'Content-Type': 'application/json'
        }
      })
      .then(response => {
        this.selectedInterests = response.data.data.categories
      })
      .catch(error => {
        console.error('失败', error);
      });
    },
    modifyUserInterestData() {
      let modifyInterestData = {
        'userAccount': this.$store.state.user.userAccount,
        'categories': this.selectedInterests,
      }
      this.$api.serviceUser.post('api/user/modifyUserInterest', modifyInterestData, {
        headers: {
          'Content-Type': 'application/json'
        }
      })
      .then(response => {
        if (response.data.code == 200){
          this.$centerMessage({
            message: '修改用户兴趣爱好成功',
            type: 'success'
          });
        }
      })
      .catch(error => {
        console.error('失败', error);
      });
    },
  },
  mounted() {
    this.queryUserInterestData();
  },
}
</script>

<style scoped>
.user-interest {
  background-color: white;
  max-width: 600px;
  margin-left: 50px;
}

h2 {
  color: #333;
  text-align: center;
  margin-bottom: 20px;
}

.interest-form {
  display: grid;
  grid-template-columns: repeat(4, 1fr); /* 改为4列 */
  gap: 10px;
}

.interest-option {
  padding: 10px;
  font-size: 14px;
  border: 1px solid #ddd;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
  text-align: center;
  background-color: white;
  color: #333;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  box-sizing: border-box;
}

.interest-option:hover {
  background-color: #f5f5f5;
}

.interest-option.selected {
  background-color: #fff2f0;
  color: #ff4d4f;
  border-color: #ff4d4f;
}

.submit-btn {
  display: block;
  margin: 25px auto 0;
  padding: 10px 30px;
  background-color: #ff4d4f;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

.submit-btn:hover {
  background-color: #ff7875;
}
</style>